package test.ground.monitor;

import com.wang.tool.monitor.InvokeTemplate;
import com.wang.tool.monitor.LinkLogUtil;
import com.wang.tool.monitor.core.LinkHolder;
import com.wang.tool.monitor.core.MonitorInfo;
import com.wang.tool.monitor.template.AbsMonitorInvoker;
import com.wang.tool.executor.transitivethreadcontext.ThreadContextTransferTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author wangshaopeng
 * @Date 2022/7/2
 */
@Service
public class ServiceC {

    @Autowired
    private ServiceB serviceB;

    @Autowired
    private ServiceA serviceA;

    private static ExecutorService executor = ThreadContextTransferTool.wrap(Executors.newFixedThreadPool(30));
    private static ExecutorService executor2 = Executors.newFixedThreadPool(30);


    public String hi(String k) {
//        if(true){
//            for (int i = 0; i < 1; i++) {
//                serviceA.hi(k);
//            }
//            return "1";
//        }
//
//        if(true){
//            ArrayList<Runnable> runnables = new ArrayList<>();
//            for (int i = 0; i < 9; i++) {
//                runnables.add(new Runnable() {
//                    @Override
//                    public void run() {
//                        serviceA.hi(k);
//                    }
//                });
//            }
//
//            ConcurrentInvoker.waitRun("testAction",executor,false,50,runnables);
//            return null;
//        }
//
//
//        LinkInfo current = InvokeTemplate.getCurrent();
//
//
//        executor.execute(()->{
//            InvokeTemplate.invoke(current, "testg", "biz", "ac", new MonitorInvoker<String>() {
//                @Override
//                public String internalCall() throws Throwable {
//
//
//                    InvokeTemplate.supplementLink(InvokeTemplate.getCurrent(), "g", "b", "a", System.currentTimeMillis(), null, System.currentTimeMillis(), "2", null
//                            , new MonitorWeaver<String>() {
//                                @Override
//                                public void weaveOnBefore(MonitorInfo<String> monitorInfo) {
//                                    System.out.println("前");
//                                }
//
//                                @Override
//                                public void weaveOnAfter(MonitorInfo<String> monitorInfo) {
//                                    System.out.println("后");
//
//                                }
//                            });
//                    return null;
//                }
//            });
//        });
//
//
//        try {
//            Thread.sleep(10);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//

        return InvokeTemplate.monitor("ServiceC", "hi", new AbsMonitorInvoker<String>() {
            @Override
            protected void internalAfter(MonitorInfo<String> monitorInfo) {
                monitorInfo.markImplicitSlow();
            }

            @Override
            public String internalCall() throws Exception {
                for (int i = 0; i < 1; i++) {
                    serviceA.hi("1");
                }
                LinkLogUtil.newTagLog("测试时间")
                        .v1("1")
                        .addAttachment("haha",24)
                        .log();
                LinkHolder.addTips(()->"这时测试调试西溪"+ LinkHolder.getCurrent().getLinkid());

                LinkLogUtil.newTagLog("测试时间2")
                        .addAttachment("haha",24)
                        .log();
                return "2";
            }

            @Override
            public void weaveOnBefore(MonitorInfo<String> monitorInfo) {
                monitorInfo.markDebug(true);
                monitorInfo.addDigest("t3", "4");
//
                monitorInfo.addDigest("te2", null);
                monitorInfo.addDetailResult(234);

            }
        });

    }

    public static void main(String[] args) {
        System.out.println("21364ffb16759067687702638ebe91".length());
    }


}
